home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d22 / diskedit.arc / VIDEO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-06  |  5.3 KB  |  178 lines

  1. /* video.c - a few video subroutines to make writing C programs
  2.              a bit simpler
  3.  
  4.         a line of code for printing something will look like this:
  5.  
  6.             at(2,20); in(RED,WHITE); cprintf("Just Like English!");
  7.  
  8.         Of course, you can omit the at() or in() (or both) if you like.
  9.  
  10.     NOTE: Remember that you must somehow force VIDEO.C to recompile
  11.           whenever you switch memory models. This is because I haven't
  12.           specifically declared all the routines and variables as 'far'
  13.           (I hate using extra space and code unless I have to). If you
  14.           like, you can do so yourself, then you can link the same OBJ
  15.           with any memory model.
  16.  
  17.     To use the following routines in your own program, just put the
  18.     files video.c and video.hin your working directory,
  19.     add the line '#include "video.h" to the top of your program, call
  20.     initvideo() at the beginning of main(), and link video.obj with 
  21.     your main module.
  22.  
  23.     This has been tested, and runs without modification, on both
  24.     Zortech C++ 1.0? and Turbo C 1.5. I don't know about other compilers
  25.     or versions, but I imagine they shouldn't need much modification.
  26.  
  27.     As usual: permission granted to use this for whatever you want to,
  28.     commercial, private, obscene, or otherwise.
  29.  
  30.                                     Laine Stump, 12/13/87
  31.                                                                         */
  32. #include <stdarg.h>
  33. #include <stdio.h>
  34. #include <dos.h>
  35. #include <string.h>
  36. #include "video.h"
  37.  
  38. typedef unsigned int WORD;
  39. typedef unsigned char BYTE;
  40.  
  41. int  curcolor;
  42. int  currow, curcol;
  43. int scrrows, scrcols;
  44. WORD far *screen;
  45. char nibs[] = "0123456789ABCDEF";
  46.  
  47. int screenrows(void) { return(scrrows); }
  48. int screencols(void) { return(scrcols); }
  49.  
  50. void scroll(int lines, int x1, int y1, int x2, int y2)
  51.   { /* Scrolls the screen between x1,y1 and x2,y2. Scrolling is up if
  52.      lines is positive, down if it is negative */
  53.   union REGS reg;
  54.  
  55.   if (lines < 0)
  56.     {
  57.     reg.h.ah = 7;
  58.     lines = (-lines);
  59.     }
  60.   else
  61.     reg.h.ah = 6;
  62.   reg.h.al = lines;
  63.   reg.h.bh = curcolor >> 8;
  64.   reg.x.cx = (y1 << 8) + x1;
  65.   reg.x.dx = (y2 << 8) + x2;
  66.   int86(0x10, ®, ®);
  67.   } /* scroll */
  68.  
  69. void cursorsize(int startline, int endline)
  70.   {  /* Sets the size of the cursor */
  71.     union REGS reg;
  72.  
  73.   reg.h.ah = 1;
  74.   reg.x.cx = (startline << 8) + endline;
  75.   int86(0x10, ®, ®);
  76.   } /* cursorsize */
  77.  
  78. void clearscreen(void)
  79.   { /* clears the screen to curcolor and positions cursor at(0,0) */
  80.   scroll(0, 0, 0, scrcols, scrrows);
  81.   at(0,0);
  82.   } /* clearscreen */
  83.  
  84. void cleareol(void)
  85.   { /* clears from current position to end of line */
  86.   scroll(0,curcol, currow, scrcols, currow);
  87.   }
  88.  
  89. void at(int row, int col)
  90.   { /* places the cursor at a specific row and column */
  91.   union REGS reg;
  92.  
  93.   currow = row; curcol = col;
  94.   reg.h.ah = 2;
  95.   reg.h.bh = 0;
  96.   reg.x.dx = (row << 8) + col;
  97.   int86(0x10, ®, ®);
  98.   } /* at */
  99.  
  100. void in(char forecolor, char backcolor)
  101.   { /* sets current color to 'color' */
  102.   curcolor = (forecolor | (backcolor << 4)) << 8;
  103.   } /* in */
  104.  
  105. void cputc(char ch)
  106.   {
  107.   WORD far *ptr;
  108.  
  109.   *((WORD far *) (screen + (currow * scrcols) + curcol))
  110.     = ch+curcolor;
  111.   at(currow, curcol+1);
  112.   } /* cputc */
  113.  
  114. void cprintf(va_list arg_list, ...)
  115. /* Prints a string in video memory at current location in current color
  116.    then advances cursor to end of string. This cprintf is functionally
  117.    equivalent to the standard printf. the only difference is that it will 
  118.    use the color set by the function in(). (and that it is faster...)
  119. */
  120.   {
  121.   va_list arg_ptr;
  122.   char *format;
  123.   char output[81];
  124.   BYTE* outptr;
  125.   WORD far* scrptr;
  126.   int ct, len;
  127.  
  128.   va_start(arg_ptr, arg_list);
  129.   format = arg_list;
  130.   vsprintf(output, format, arg_ptr);
  131.   len = strlen(output);
  132.   scrptr = screen + (currow * scrcols) + curcol;
  133.   outptr = (BYTE *) output;
  134.   for (ct = 0; ct < len; ct++)
  135.     *scrptr++ = (*outptr++)+curcolor;
  136.   at(currow,curcol+len);
  137.   } /* cprintf */
  138.  
  139. void Write16Bytes (void* buf)
  140.   { /* write the 16 bytes at *buf as hex digits, followed by the
  141.        ASCII equivalent of same, at the curent cursor location.
  142.        This routine is a bit specialized to have in a general
  143.        purpose library, but I wanted faster screen paints. That's
  144.        one of the advantages of writing your own library - you get
  145.        to change it! */
  146.   int ct;
  147.   BYTE* this;
  148.   WORD far * scrptr;
  149.   
  150.   this = (BYTE *) buf;
  151.   scrptr = (WORD far *) (screen + (currow * scrcols) + curcol);
  152.   for(ct = 0; ct < 16; ct++)
  153.     {
  154.     *scrptr++ = nibs[(*(this)) >> 4]+curcolor;
  155.     *scrptr++ = nibs[(*this++) & 0x0F]+curcolor;
  156.     scrptr++;
  157.     }
  158.   this = buf;
  159.   for(ct = 0;  ct < 16; ct++)
  160.     *scrptr++ = curcolor
  161.                 + (*this > 0x1F ? *(this++) : (*(this++),'.'));
  162.   at(currow,curcol+65);
  163.   }    /* Write16Bytes */
  164.  
  165. void initvideo(void)
  166.   { /* this should be called once at the beginning of the application */
  167.   union REGS reg;
  168.   reg.h.ah = 15;
  169.   int86(0x10, ®, ®);
  170.   scrcols = reg.h.ah;
  171.   scrrows = *((BYTE far *) MK_FP(0x0040,0x0084))+1;
  172.   if (scrrows < 25) scrrows = 25;
  173.   screen = (WORD far *)((reg.h.al != 7) ? 0xB8000000L : 0xB0000000L);
  174.   in(LIGHTGRAY,BLACK); clearscreen();
  175.   } /* initvideo */
  176.  
  177. /*---- end of video.c ----*/
  178.